'''
Created on Jul 9, 2011

@author: kafkef
'''

from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL, Qt, QSettings, QVariant


from dexen.system.client import ui, db
from dexen.system.client.ui.job_page import JobTabWidget
from dexen.system.client.ui.job_navigator import JobNavigatorWidget


svr_proxy = ui.svr_proxy

class JobWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self._create_widgets()
    
    def get_navigator(self):
        return self.navigator
    
    def _create_widgets(self):
        #create widgets
        self.navigator = JobNavigatorWidget()
        self.tab_widget = JobTabWidget()

        settings = QSettings()

        #setup layout
        layout = QHBoxLayout()
        self.splitter = QSplitter(self)
        
        self.splitter.addWidget(self.navigator)
        self.splitter.addWidget(self.tab_widget)
        self.splitter.restoreState(settings.value("MainWindow/Splitter").toByteArray())
        layout.addWidget(self.splitter)
        self.setLayout(layout)

        #connect signal/slots        
        self.connect(self.navigator, SIGNAL("cur_job_changed"), 
                     self.on_cur_job_changed)
    
    def on_server_connected(self):
        self.navigator.on_server_connected()
    
    def on_server_disconnected(self, svr_info):
        self.navigator.on_server_disconnected(svr_info)
    
    def on_cur_job_changed(self, job_info):
        assert isinstance(job_info, db.JobInfo)
        print "JobWidget:on_cur_job_changed:", job_info.get_id()
        self.setCursor(QCursor(Qt.WaitCursor))
        self.tab_widget.on_cur_job_changed(job_info)
        self.setCursor(QCursor(Qt.ArrowCursor))
    
    def on_server_info_initialized(self, svr_info_list):
        print "JobWidget:on_server_info_initialized"
        self.navigator.on_server_info_initialized(svr_info_list)
        self.tab_widget.on_server_info_initialized(svr_info_list)
        
    def on_server_info_updated(self, svr_info):
        print "JobWidget:on_server_info_updated"
        self.navigator.on_server_info_updated(svr_info)
        self.tab_widget.on_server_info_updated(svr_info)
        
    def on_actions_received(self, job_info, actions):
        print "JobWidget:on_actions_received"
        self.tab_widget.on_actions_received(job_info, actions)
        
    def on_actions_initialized(self, job_info, actions):
        print "JobWidget:on_actions_initialized"
        self.tab_widget.on_actions_initialized(job_info, actions)
        
    def on_inds_initialized(self, job_info, inds):
        self.tab_widget.on_inds_initialized(job_info, inds)
    
    def on_inds_updated(self, job_info, inds):
        self.tab_widget.on_inds_updated(job_info, inds)
        
    def save_state(self):
        print "saving state in job_main"
        settings = QSettings()
        settings.setValue("MainWindow/Splitter", self.splitter.saveState())
    
